package me.fung.rem.app.auth

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.material.Checkbox
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Lock
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.PasswordVisualTransformation
import me.fung.keen.app.view.View
import me.fung.rem.component.TextInput

class SignInView : View<SignInPresenter>() {
    @Composable
    override fun content(modifier: Modifier) {
        val data = remember { p.data }
        LaunchedEffect(Unit) {
            p.loadLocalFile()
        }
        Box(modifier = modifier, contentAlignment = Alignment.Center) {
            Column() {
                TextInput(
                    placeholder = "输入开门密码",
                    onSubmit = {
                        p.data.input = it
                        p.signIn()
                    },
                    buttonContent = {
                        Text("登录")
                    },
                    initText = data.input,
                    requestFocuses = true,
                    visualTransformation = PasswordVisualTransformation(),
                    leadingIcon = {
                        Icon(
                            Icons.Default.Lock,
                            contentDescription = null,
                            tint = Color.Black,
                        )
                    },
                    modifier = Modifier,
                )
                Row {
                    Row(
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Checkbox(
                            checked = data.autoSignIn,
                            onCheckedChange = {
                                p.data.autoSignIn = it
                            }
                        )
                        Text("自动登录", modifier = Modifier.clickable { p.data.autoSignIn = !p.data.autoSignIn })
                    }
                }
            }
        }
    }
}